package com.mycompany.biblio.controller;


import com.mycompany.biblio.business.SecurityService;
import com.mycompany.biblio.business.StatusDAO;
import com.mycompany.biblio.business.UserDAO;
import com.mycompany.biblio.model.Status;
import com.mycompany.biblio.model.User;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

@ManagedBean
@SessionScoped
public class ProfileController
{

    // ======================================
    // =             Attributes             =
    // ======================================

    @EJB
    private UserDAO userEJB;

    private User user= new User();
    
    @EJB
    private StatusDAO statusEJB;
    
    @EJB
    private SecurityService securityService;
    
    private Status status = new Status();
    private ListDataModel statusList;


    // ======================================
    // =           Public Methods           =
    // ======================================

    public String doprofile() {
       
        return "accueil.xhtml";
    }

        public void doNew() 
    {
        status = new Status();        
    }

    public String doCreate()
    {       
        try
        {            
            status.setDatePublication(new Date());
            status.setProprietaire(securityService.getCurrentUser());
            status = statusEJB.create(status);            
        }
        catch (Exception ex)
        {              
            return null;
        }
        doNew();
        return "/private/actualites.xhtml";
    }
    
   
    
    public String doCancel() {
        return "accueil.xhtml";
    }

    public String doEdit() {
        status = (Status)statusList.getRowData(); // Voici comment on trouve le livre sélectionné
        return "editStatus.xhtml?faces-redirect=true";
    }

    public String doSave() {
        status = statusEJB.update(status);
        return "listStatuss.xhtml?faces-redirect=true";
    }
  
    // ======================================
    // =          Getters & Setters         =
    // ======================================

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
      public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

}

